#include "win32apis.h"

using namespace utils;

HWND Wallpaper::hWorkerw = nullptr;

void Wallpaper::setWindowWallpaper(HWND hwnd, bool autoFullscreen)
{
    SetParent(hwnd, GetWorkerW());
    if (autoFullscreen) {
        win32apis::ShowFullScreen(hwnd);
    }
}

void Wallpaper::setWindowWallpaper(int winID, bool autoFullscreen)
{
    setWindowWallpaper((HWND)winID, autoFullscreen);
}

BOOL Wallpaper::EnumWindowsProc(HWND tophandle, LPARAM topparamhandle)
{
    HWND defview = FindWindowEx(tophandle, 0, L"SHELLDLL_DefView", nullptr);
    if (defview != nullptr)
    {
        hWorkerw = FindWindowEx(0, tophandle, L"WorkerW", 0);
    }
    return true;
}

HWND Wallpaper::GetWorkerW()
{
    int result;
    HWND windowHandle = FindWindow(L"Progman", nullptr);
    SendMessageTimeout(windowHandle, 0x052c, 0 ,0, SMTO_NORMAL, 0x3e8,(PDWORD_PTR)&result);
    EnumWindows(EnumWindowsProc,(LPARAM)nullptr);
    ShowWindow(hWorkerw,SW_HIDE);
    return windowHandle;
}

void win32apis::ShowFullScreen(HWND hwnd)
{
    LONG style = GetWindowLong(hwnd, GWL_STYLE);
    style |= WS_DLGFRAME;
    style &= ~WS_CAPTION;
    style &= ~WS_BORDER;
    SetWindowLong(hwnd, GWL_STYLE, style);
    int w = GetSystemMetrics(SM_CXSCREEN);
    int h = GetSystemMetrics(SM_CYSCREEN);
    MoveWindow(hwnd, 0, 0, w, h, true);
}
